<?php
/**
 * ============================================================================
 * Copyright (c) 2015-2018 贵州大师兄信息技术有限公司 All rights reserved.
 * siteַ: http://www.dsxcms.com
 * ============================================================================
 * @author:     David Song<songdewei@163.com>
 * @version:    v1.0.0
 * ---------------------------------------------
 * Date: 2018/9/18
 * Time: 下午5:36
 */

namespace App\Library\Mall;


use App\Library\AuthenticatedUser;
use Illuminate\Http\Request;

trait ItemTrait
{
    use AuthenticatedUser;

    public function store(Request $request)
    {
        $catid   = intval($request->input('catid'));
        $itemid  = intval($request->input('itemid'));
        $newitem = $request->post('newitem');
        if ($newitem['title'] && $newitem['price'] && $catid){
            $newitem = array_only($newitem, [
                'title',
                'subtitle',
                'price',
                'market_price',
                'stock',
                'shipping_fee',
                'on_sale',
                'thumb',
                'image'
            ]);
            $newitem['catid'] = $catid;
            //产品图片
            $images = $request->input('images');
            $firstImg = array_first($images);
            if (isset($firstImg['thumb'])){
                $newitem['thumb'] = $firstImg['thumb'];
                $newitem['image'] = $firstImg['image'];
            }

            if ($itemid) {
                $item = $this->user()->items()->find($itemid);
                $item->update($newitem);
            }else {
                $shop = $this->user()->shop;
                $newitem['shop_id'] = $shop->shop_id;
                $newitem['sn'] = createItemSn();
                $item = $this->user()->items()->create($newitem);
            }

            if ($images) {
                foreach ($images as $image){
                    $id = intval($image['id']);
                    if ($id > 0) {
                        if ($image['thumb'] && $image['image']){
                            $item->images()->where('id', $id)->update([
                                'thumb'=>$image['thumb'],
                                'image'=>$image['image']
                            ]);
                        } else {
                            $item->images()->where('id', $id)->delete();
                        }

                    }else {
                        if ($image['thumb'] && $image['image']){
                            $item->images()->create([
                                'thumb'=>$image['thumb'],
                                'image'=>$image['image']
                            ]);
                        }
                    }
                }
            }

            //添加产品介绍
            $content = array_only($request->post('content'), ['content']);
            if ($con = $item->content){
                $con->update($content);
            }else {
                $item->content()->create($content);
            }

            //产品属性
            $properties = $request->post('properties');
            if ($properties) {
                $catlogProps = $item->catlog->props()->orderBy('prop_id')->get();
                if ($itemid) {
                    $itemProps = [];
                    $item->props()->get()->map(function ($prop) use (&$itemProps, $item){
                        if (isset($properties[$prop->prop_id])) {
                            $itemProps[$prop->prop_id] = $prop;
                        } else {
                            $item->props()->where('id', $prop->id)->delete();
                        }
                    });

                    foreach ($catlogProps as $prop) {
                        if (isset($properties[$prop->prop_id])) {
                            $prop_value = $properties[$prop->prop_id];
                        } else {
                            $prop_value = '';
                        }

                        if ($prop_value) {
                            if (isset($itemProps[$prop->prop_id])) {
                                $item->props()->where('prop_id', $prop->prop_id)
                                    ->update([
                                        'prop_name'=>$prop->title,
                                        'prop_value'=>$prop_value
                                    ]);
                            } else {
                                $item->props()->create([
                                    'prop_id'=>$prop->prop_id,
                                    'prop_name'=>$prop->title,
                                    'prop_value'=>$prop_value
                                ]);
                            }
                        }
                    }

                } else {
                    foreach ($catlogProps as $prop) {
                        if (isset($properties[$prop->prop_id])) {
                            $prop_value = $properties[$prop->prop_id];
                        } else {
                            $prop_value = '';
                        }

                        if (is_array($prop_value)) $prop_value = implode(',', $prop_value);

                        if ($prop_value) {
                            $item->props()->create([
                                'prop_id'=>$prop->prop_id,
                                'prop_name'=>$prop->title,
                                'prop_value'=>$prop_value
                            ]);
                        }
                    }
                }
            }

            return $this->sendStoredItemResponse($request, $item);
        }else {
            return $this->sendStoredItemFailedResponse($request);
        }
    }

    protected function sendStoredItemResponse(Request $request, $item)
    {
        return ajaxReturn(['item'=>$item]);
    }

    protected function sendStoredItemFailedResponse(Request $request)
    {
        return ajaxError(201, __('sysmessage.invalid parameter'));
    }

    public function delete(Request $request)
    {
        $items = $request->input('items');
        if (is_array($items))
        {
            $this->user()->items()->whereIn('itemid', $items)->get()->map(function ($item)
            {
                $item->delete();
            });
        }
    }

    protected function sendDeletedItemResponse(Request $request)
    {
        return ajaxReturn();
    }

    public function toggleSale(Request $request)
    {
        $on_sale = intval($request->input('on_sale'));
        $items = $request->input('items');

        if (is_array($items))
        {
            $this->user()->items()->whereIn('itemid', $items)->get()->map(function ($item) use ($on_sale)
            {
                $item->on_sale = $on_sale;
                $item->save();
            });
        }

        return ajaxReturn();
    }
}
